package com.sanshow.charging.data;

public class DownCommand {

    private static String packageHeader = "16";
    private static String itemCode = "01";
    private static String heartBeat = "00"; // 心跳默认10秒
    private static String charging = "00"; // 充电或不充电(0XC1充电; 0xC2不充电)
    private static String lighting = "00"; // 照明(0XD1亮；0XD2灭)
    private static String packageTail = "DF";

    // 门指令
    public static String doorDirectives = "A1";
    // 心跳指令
    public static String heartBeatDirectives = "B1";
    // 充电指令
    public static String chargingDirectives = "C1";
    // 灯指令
    public static String lightDirectives = "D1";

    // 开门操作
    public static String openCmd = "00";
    // 心跳操作
    public static String heartBeatCmd = "00";
    // 充电操作
    public static String chargingCmd = "00";
    // 断电操作
    public static String blackoutCmd = "01";
    // 开灯操作
    public static String openLightingCmd = "00";
    // 关灯操作
    public static String closeLightingCmd = "01";


    public static String getOpenDoorCmdHex(String itemCode) {
        StringBuilder sbCmd = new StringBuilder();
        sbCmd.append(packageHeader);
        sbCmd.append(itemCode);
        sbCmd.append(doorDirectives);
        sbCmd.append(openCmd);
        sbCmd.append(packageTail);
        sbCmd.append(packageTail);
        return sbCmd.toString();
    }

    public static String getOpenDoorCmdHexForLocal(String itemCode) {
        String strCmd = "";
        int i = Integer.parseInt(itemCode);

        if (i <= 9) {
            strCmd = "0" + i ;
        }

        if (9 < i && i <= 15) {
            strCmd = "0" + Integer.toHexString(i) ;
        }

        if (i > 15) {
            strCmd =  Integer.toHexString(i);
        }

        StringBuilder sbCmd = new StringBuilder();
        sbCmd.append(packageHeader);
        sbCmd.append(strCmd);
        sbCmd.append(doorDirectives);
        sbCmd.append(openCmd);
        sbCmd.append(packageTail);
        sbCmd.append(packageTail);
        return sbCmd.toString();
    }


    public static String getHeartBeatCmdHex(String itemCode) {
        StringBuilder sbCmd = new StringBuilder();
        sbCmd.append(packageHeader);
        sbCmd.append(itemCode);
        sbCmd.append(heartBeatDirectives);
        sbCmd.append(heartBeatCmd);
        sbCmd.append(packageTail);
        sbCmd.append(packageTail);
        return sbCmd.toString();
    }

    public static String getChargingCmdHex(String itemCode) {
        StringBuilder sbCmd = new StringBuilder();
        sbCmd.append(packageHeader);
        sbCmd.append(itemCode);
        sbCmd.append(chargingDirectives);
        sbCmd.append(chargingCmd);
        sbCmd.append(packageTail);
        sbCmd.append(packageTail);
        return sbCmd.toString();
    }

    public static String getBlackoutCmdHex(String itemCode) {
        StringBuilder sbCmd = new StringBuilder();
        sbCmd.append(packageHeader);
        sbCmd.append(itemCode);
        sbCmd.append(chargingDirectives);
        sbCmd.append(heartBeatCmd);
        sbCmd.append(packageTail);
        sbCmd.append(packageTail);
        return sbCmd.toString();
    }

    public static String getOpenLightingCmdHex(String itemCode) {
        StringBuilder sbCmd = new StringBuilder();
        sbCmd.append(packageHeader);
        sbCmd.append(itemCode);
        sbCmd.append(lightDirectives);
        sbCmd.append(openLightingCmd);
        sbCmd.append(packageTail);
        sbCmd.append(packageTail);
        return sbCmd.toString();
    }

    public static String getCloseLightingCmdHex(String itemCode) {
        StringBuilder sbCmd = new StringBuilder();
        sbCmd.append(packageHeader);
        sbCmd.append(itemCode);
        sbCmd.append(lightDirectives);
        sbCmd.append(closeLightingCmd);
        sbCmd.append(packageTail);
        sbCmd.append(packageTail);
        return sbCmd.toString();
    }

    public static String getItemCodeForHex(String itemCode) {
        String strCmd = "";
        int i = Integer.parseInt(itemCode);

        if (i <= 9) {
            strCmd = "0" + i ;
        }

        if (9 < i && i <= 15) {
            strCmd = "0" + Integer.toHexString(i) ;
        }

        if (i > 15) {
            strCmd =  Integer.toHexString(i);
        }

        return strCmd;
    }


}
